package ben.samplegame.sprite;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Shape;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.ResourceBundle;
import java.util.Set;
import javax.imageio.ImageIO;
import javax.swing.KeyStroke;
import ben.samplegame.collision.EnemyCollisionBehavior;
import ben.samplegame.collision.PlayerCollisionBehavior;
import game.physics.GameVector;
import game.physics.ImpulseVector;
import game.sprite.PlayerSprite;
import game.sprite.collision.CollisionBehavior;
import game.sprite.collision.ICollidable;
import game.userinteraction.IControllable;
import game.userinteraction.UserInputBehavior;


public class SamplePlayer extends PlayerSprite implements ICollidable, IControllable {

    private final String STANDARD_STATE = "standard_state";
    private CollisionBehavior myCollisionBehavior;
    private UserInputBehavior myBehavior;

    public SamplePlayer (Point startLocation, String spriteSet, boolean isActive)
            throws SecurityException,
            NoSuchMethodException {

        super(startLocation, spriteSet, isActive);
        myCollisionBehavior = new PlayerCollisionBehavior(this);
        myBehavior = new UserInputBehavior(this);
        myBehavior.defineKeyBindings(ResourceBundle.getBundle("resources.UserInteraction"));
        HashMap<String, Image> tempSpriteSet = new HashMap<String, Image>();
        Image testImage = null;
        try {
            testImage = ImageIO.read(new File("src/ben/samplegame/resources/duvall_ship.png"));

        }
        catch (IOException e) {
        }

        tempSpriteSet.put(STANDARD_STATE, testImage);
        setSpriteSet(tempSpriteSet);
        setActiveImage(STANDARD_STATE);
        setCenterCoordinate(startLocation);
        // this.setRotation(120);
        this.resize(1);

    }

    @Override
    public void applyFriction () {
        getVelocity().multiply(.2);

    }

    @Override
    public void update () {
        move();
        applyFriction();
    }

    @Override
    public void applyForce (GameVector force) {

    }

    @Override
    public Shape getOutline () {
        return getBounds();
    }

    @Override
    public CollisionBehavior getCollisionBehavior () {
        return myCollisionBehavior;

    }

    @Override
    public void setCollisionBehavior (CollisionBehavior collisionBehavior) {
        myCollisionBehavior = collisionBehavior;
    }

    @Override
    public void accept (ICollidable collidable) {
        collidable.getCollisionBehavior().visit(this);

    }

    @Override
    public void react (Set<KeyStroke> keyPresses) {
        myBehavior.react(keyPresses);

    }

    public void moveLeft () {
        this.addExternalForce(new ImpulseVector(-1, 0, .3));
        System.out.println("controls work!");
    }

    public void moveRight () {
        this.addExternalForce(new ImpulseVector(1, 0, .3));
    }

    public void moveUp () {
        this.addExternalForce(new ImpulseVector(0, -1, .3));
    }

    public void moveDown () {
        this.addExternalForce(new ImpulseVector(0, 1, .3));
    }

    public void rotateLeft () {
        setRotation(getRotation() + 7);
    }

    public void rotateRight () {
        setRotation(getRotation() - 7);
    }

}
